import math, os, random, sys
import jieba
from properties import Properties


def progress(percent, lines, width=50):
    if percent >= 100:
        percent = 100
    show_str = ('[%%-%ds]' % width) % (int(width * percent / 100) * "#")
    print('\r%s %d%% [%5d]' % (show_str, percent,lines), end='')


def _convert_seq2seq_files(props, questions, answers):
    test_set_size = int(props.getProperties('Attrs.test_set_size'))
    # 创建文件
    train_enc = open(props.getProperties('Path.train.train_enc'), 'w', encoding='utf-8')  # 问
    train_dec = open(props.getProperties('Path.train.train_dec'), 'w', encoding='utf-8')  # 答
    test_enc = open(props.getProperties('Path.test.test_enc'), 'w', encoding='utf-8')  # 问
    test_dec = open(props.getProperties('Path.test.test_dec'), 'w', encoding='utf-8')  # 答

    test_index = random.sample([i for i in range(len(questions))], test_set_size)
    print('start processing ask-answer data sets')
    for i in range(len(questions)):
        if i in test_index:
            test_enc.write(questions[i] + '\n')
            test_dec.write(answers[i] + '\n')
        else:
            train_enc.write(questions[i] + '\n')
            train_dec.write(answers[i] + '\n')
        if i % 1000 == 0:
            progress(i*100 / len(questions), i)

    print('\n')
    train_enc.close()
    train_dec.close()
    test_enc.close()
    test_dec.close()


def process_shooter_data():
    props = Properties('standard.props')
    props.readProperties()
    conv_path = props.getProperties('Path.zipped_data')
    e = props.getProperties('Attrs.e')
    m = props.getProperties('Attrs.m')
    if not os.path.exists(conv_path):
        print('Data file is not exist.', file=sys.stderr)

    convs = []
    with open(conv_path, 'rU', encoding='utf-8') as f:
        ones_conv = []
        for line in f:
            line = line.strip('\n').replace('/', '')
            if line == '':
                continue
            if line[0] == e:
                if ones_conv:
                    convs.append(ones_conv)
                ones_conv = []
            elif line[0] == m:
                ones_conv.append(line.split(' ')[1])

    ask = []
    response = []
    for conv in convs:
        if len(conv) == 1:
            continue
        if len(conv) % 2 != 0:
            conv = conv[:-1]
        for i in range(len(conv)):
            if i % 2 == 0:
                conv[i] = " ".join(jieba.cut(conv[i]))
                ask.append(conv[i])
            else:
                conv[i] = " ".join(jieba.cut(conv[i]))
                response.append(conv[i])

    _convert_seq2seq_files(props, ask, response)
